package bsc.war.ajax;

import bsc.ejb.entity.Comparator;
import bsc.ejb.entity.ComparatorDimensionalCut;
import bsc.ejb.entity.DimensionMember;
import bsc.ejb.entity.DimensionalCut;
import bsc.ejb.entity.Pointer;
import bsc.war.delegate.DelegateBSC;
import bsc.war.factory.Factory;
import bsc.war.handler.DrillDashboard;
import bsc.war.service.ScoreCardUtils;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

/**
 *
 * @author Memo
 */
public class AjaxDrillDashboard extends AbstractController {

    public AjaxDrillDashboard() {
    }

    @Override
    protected ModelAndView handleRequestInternal(
            HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        try {
            DelegateBSC delegate = (DelegateBSC) Factory.getBean("DelegateBSC");

            DrillDashboard.processRequestInternal(request, response, delegate);

        } catch (Exception e) {
            logger.error("Catched exception", e);
        }
        return new ModelAndView("ajaxDrillDashboard");
    }
    /*
    public static void processRequestInternal(HttpServletRequest request,
            HttpServletResponse response, DelegateBSC delegate) {
        //Find the scorecard and dimensionalCut
        Long idPointer = Long.parseLong(request.getParameter("pointer"));
        Long idDimensionalCut = Long.parseLong(request.getParameter("dimensionalCut"));

        //Info Pointer - DimensionalCut - Members
        Pointer pointer = (Pointer) delegate.findScoreCard(idPointer);

        //RootDimensionalCut
        DimensionalCut rootDimensionalCut = (DimensionalCut) delegate.findDimensionalCut(idDimensionalCut);
        List<DimensionMember> rootDimensionMembers = delegate.findDimensionMember(rootDimensionalCut);

        //Find the cuts of the pointer
        List<DimensionalCut> dimensionalCuts = delegate.findDimensionalCut((Pointer) pointer);
        List<DimensionalCut> rootDimensionalCuts = new ArrayList<DimensionalCut>();

        //Members of the cut
        Map<DimensionalCut, List<DimensionMember>> cutMembers = new HashMap<DimensionalCut, List<DimensionMember>>();
        //Members Strng of the Cut
        Map<DimensionalCut, String> cutMembersString = new HashMap<DimensionalCut, String>();


        for (DimensionalCut dimensionalCut : dimensionalCuts) {
            List<DimensionMember> dimensionMembers = delegate.findDimensionMember(dimensionalCut);
            //System.out.println("El tamaño del corte: " + i + " es de "+ dimensionMembers.size());
            if (ScoreCardUtils.reviewRootAdvance(dimensionMembers, rootDimensionMembers)) {
                cutMembers.put(dimensionalCut, dimensionMembers);
                cutMembersString.put(dimensionalCut, ScoreCardUtils.createMembersString(dimensionMembers));
                rootDimensionalCuts.add(dimensionalCut);
            }
        }

        //Comparator of the pointer
        List<Comparator> comparators = delegate.findComparator(pointer);

        //ComparatorsDimensionalCut of the cut
        Map<DimensionalCut, List<ComparatorDimensionalCut>> dataComparators = new HashMap<DimensionalCut, List<ComparatorDimensionalCut>>();



        //Definition of the map - DimensionalCut - Dates
        Map<DimensionalCut, List<Date>> dimensionalCutDates = new HashMap<DimensionalCut, List<Date>>();

        //Definition of the map - DimensionalCut - Map ( Date- ComparatorDimensionalCut )
        Map<DimensionalCut, Map<Date, List<ComparatorDimensionalCut>>> dimensionalCutDatesData = new HashMap<DimensionalCut, Map<Date, List<ComparatorDimensionalCut>>>();

        for (DimensionalCut dimensionalCut : rootDimensionalCuts) {

            List<Date> dimensionalCutDate = delegate.findComparatorDimensionalCutDate(dimensionalCut);

            Map<Date, List<ComparatorDimensionalCut>> comparatorDimensionalCutData = new HashMap<Date, List<ComparatorDimensionalCut>>();
            for (Date date : dimensionalCutDate) {
                List<ComparatorDimensionalCut> comparatorDimensionalCuts = delegate.findComparatorDimensionalCut(dimensionalCut, date);
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                comparatorDimensionalCutData.put(calendar.getTime(), comparatorDimensionalCuts);
                //System.out.println("Fecha del mapa: " + calendar.getTime());
           
            }

            dimensionalCutDates.put(dimensionalCut, dimensionalCutDate);
            dimensionalCutDatesData.put(dimensionalCut, comparatorDimensionalCutData);

            List<ComparatorDimensionalCut> comparatorDimensionalCuts = delegate.findComparatorDimensionalCut(dimensionalCut);
            dataComparators.put(dimensionalCut, comparatorDimensionalCuts);
        }

        
        //Se manda a llamar el método que genera las fechas
        //List<Date> rangeDates= ScoreCardUtils.generateDates(ScoreCardUtils.getSystemDate(request));


        request.setAttribute("pointer", pointer);
        request.setAttribute("dimensionalCuts", dimensionalCuts);
        request.setAttribute("cutMembers", cutMembers);
        request.setAttribute("comparators", comparators);
        request.setAttribute("rootDimensionalCuts", rootDimensionalCuts);
        request.setAttribute("dataComparators", dataComparators);
        request.setAttribute("dimensionalCutDates", dimensionalCutDates);
        request.setAttribute("dimensionalCutDatesData", dimensionalCutDatesData);

        request.setAttribute("rootDimensionalCut", rootDimensionalCut);
        //request.setAttribute("rangeDates", rangeDates);
        request.setAttribute("cutMembersString", cutMembersString);
    }*/
}
